package com.smartEdu.RongYun;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.util.JSONPObject;
import com.smartEdu.model.FindFriendModel;
import com.smartEdu.utils.JsonUtil;

//import jdk.nashorn.internal.runtime.JSONListAdapter;

public class RongYun1 {
	  public  Boolean SendSystemInfo(String fromuserid,String touserid,String name,String content,String url){
	    	Map jsonMap=null;
	        StringBuffer res = new StringBuffer();  
	        Boolean flag=false;
	       // String url = "http://api.cn.ronghub.com/group/create.json";  
	        HttpClient httpClient = new DefaultHttpClient();  
	        HttpPost httpPost = new HttpPost(url);  
	        httpPost.setHeader("App-Key", Tag_Rong.App_Key);  
	        httpPost.setHeader("Timestamp", Tag_Rong.Timestamp);  
	        httpPost.setHeader("Nonce", Tag_Rong.Nonce);  
	        httpPost.setHeader("Signature", Tag_Rong.Signature); 
	        httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");  
	        List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(1);  
	        nameValuePair.add(new BasicNameValuePair("fromUserId",fromuserid));  
	        nameValuePair.add(new BasicNameValuePair("toUserId",touserid));
	        nameValuePair.add(new BasicNameValuePair("objectName",name));
	        nameValuePair.add(new BasicNameValuePair("content",content));
	        HttpResponse httpResponse = null;  
	       try {  
	            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair,"utf-8"));  
	            httpResponse = httpClient.execute(httpPost);  
	            HttpEntity entity = httpResponse.getEntity(); 
	            String jsonStr = EntityUtils.toString(entity);   
	            System.out.println(jsonStr);
	            jsonMap=JsonUtil.toMap(jsonStr);
	            if(jsonMap.get("code").equals(200)){
	            	flag=true;
	            }else{
	            	flag=false;
	            }
	            
//	            JSONObject obj=JSONObject.fromObject(jsonStr);
//	            token=obj.getString(jsonMap.get("token").toString()); 
	            
	       } catch (IOException e) {  
	           e.printStackTrace();  
	        }  
	       return flag;
	    	
	    }
	  
	  public  String QueryGroup(String groupid){
		    String jsonStr=null;
	    	Map jsonMap=null;
	        StringBuffer res = new StringBuffer();  
	        Boolean flag=false;
	        String url = "http://api.cn.ronghub.com/group/user/query.json";
	        HttpClient httpClient = new DefaultHttpClient();  
	        HttpPost httpPost = new HttpPost(url);  
	        httpPost.setHeader("App-Key", Tag_Rong.App_Key);  
	        httpPost.setHeader("Timestamp", Tag_Rong.Timestamp);  
	        httpPost.setHeader("Nonce", Tag_Rong.Nonce);  
	        httpPost.setHeader("Signature", Tag_Rong.Signature); 
	        httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");  
	        List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(1);   
	        nameValuePair.add(new BasicNameValuePair("groupId",groupid));
	   
	        HttpResponse httpResponse = null;  
	       try {  
	            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair,"utf-8"));  
	            httpResponse = httpClient.execute(httpPost);  
	            HttpEntity entity = httpResponse.getEntity(); 
	            jsonStr = EntityUtils.toString(entity); 
	            jsonMap=JsonUtil.toMap(jsonStr);
	            if(jsonMap.get("code").equals(200)){
	               Object str=jsonMap.get("users");
	               jsonStr =JsonUtil.toJson(str);
	               System.out.println(jsonStr);
	            }else{
	            	jsonStr=null;
	            }     
	       } catch (IOException e) {  
	           e.printStackTrace();  
	        }  
	       return jsonStr; 	
	    }
	  
	  public Boolean isInGroup(String groupid,String json){
		  Boolean flag=false;
		  
		  ObjectMapper objectMapper = new ObjectMapper();
		    try {
		        List<LinkedHashMap<String, Object>> list = objectMapper.readValue(json, List.class);
		        for (int i = 0; i < list.size(); i++) {
		            Map<String, Object> map = list.get(i);
		            Set<String> set = map.keySet();
		            for (Iterator<String> it = set.iterator();it.hasNext();) {
		                String key = it.next();
		                if(map.get(key).equals(groupid)){
		                	flag=true;
		                	break;
		                }
		            }
		        }
		    } catch (Exception e) {
		        e.printStackTrace();
		    }
		  return flag;
	  }
	  
	  public  Boolean RetireOrCleanGroup(String userId,String groupId,String url){
	    	Map jsonMap=null;
	        StringBuffer res = new StringBuffer();  
	        Boolean flag=false;
	       // String url = "http://api.cn.ronghub.com/group/create.json";  
	        HttpClient httpClient = new DefaultHttpClient();  
	        HttpPost httpPost = new HttpPost(url);  
	        httpPost.setHeader("App-Key", Tag_Rong.App_Key);  
	        httpPost.setHeader("Timestamp", Tag_Rong.Timestamp);  
	        httpPost.setHeader("Nonce", Tag_Rong.Nonce);  
	        httpPost.setHeader("Signature", Tag_Rong.Signature); 
	        httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");  
	        List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(1);  
	        nameValuePair.add(new BasicNameValuePair("userId",userId));  
	        nameValuePair.add(new BasicNameValuePair("groupId",groupId));
	        HttpResponse httpResponse = null;  
	       try {  
	            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair,"utf-8"));  
	            httpResponse = httpClient.execute(httpPost);  
	            HttpEntity entity = httpResponse.getEntity(); 
	            String jsonStr = EntityUtils.toString(entity);   
	            System.out.println(jsonStr);
	            jsonMap=JsonUtil.toMap(jsonStr);
	            if(jsonMap.get("code").equals(200)){
	            	flag=true;
	            }else{
	            	flag=false;
	            }
	            
//	            JSONObject obj=JSONObject.fromObject(jsonStr);
//	            token=obj.getString(jsonMap.get("token").toString()); 
	            
	       } catch (IOException e) {  
	           e.printStackTrace();  
	        }  
	       return flag;
	    }
	  
	  public Boolean isTrue(String id,String json){
			System.out.println(id+json);
		    Boolean flag=false;
		     ObjectMapper objectMapper = new ObjectMapper();
			    try {
			        List<LinkedHashMap<String, Object>> list = objectMapper.readValue(json, List.class);
			        for (int i = 0; i < list.size(); i++) {
			            Map<String, Object> map = list.get(i);
			            System.out.println(map.get("qid"));
			             if(map.get("qid").toString().equals(id)){
			                flag=true;
			                break;
			               }
			        }
			    } catch (Exception e) {
			        e.printStackTrace();
			    }
			  return flag;
		  }

}
